﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MetroDataApi.Models
{
    public class recentChanges
    {
        public summaryResult maxPointThisTime { get; set; }
        public summaryResult maxPointAllTheTime { get; set; }
        public summaryResult minPointThisTime { get; set; }
        public summaryResult minPointAllTheTime { get; set; }
        public string lastTime { get; set; }
        public string originTime { get; set; }
        public string curTime { get; set; }
        public double max { get; set; }
        public double min { get; set; }
        public List<string> allTimes { get; set; }
        public int pointsCount { get; set; }
        public string title { get; set; }
        public IEnumerable<result> lastValues { get; set; }
        public IEnumerable<result> curValues { get; set; }
        public recentChanges()
        {
            this.lastValues = new List<result>();
            this.curValues = new List<result>();
            this.allTimes = new List<string>();

            this.maxPointAllTheTime = null;
            this.maxPointThisTime = null;
            this.minPointAllTheTime = null;
            this.minPointThisTime = null;
        }



        public void clear()
        {
            if (this.pointsCount != this.curValues.Count() || this.pointsCount != this.lastValues.Count())
            {
                var jj = from p in this.lastValues
                         join q in this.curValues
                         on p.name equals q.name
                         into gs
                         from g in gs.DefaultIfEmpty()
                         where g != null
                         select new
                         {
                             last = p,
                             cur = g
                         };
                this.curValues = jj.Select(p => p.cur);
                this.lastValues = jj.Select(q => q.last);
            }
        }


        public void summary()
        {
            //累积最大变化
            var maxValueAllTheTime = this.curValues.Where(p => double.IsNaN(p.value) == false).Max(g => Math.Abs(g.value));
            var maxResutlAll = this.curValues.First(g => Math.Abs(g.value) == maxValueAllTheTime);
            this.maxPointAllTheTime = new summaryResult()
            {
                name = maxResutlAll.name,
                value = Math.Round(maxResutlAll.value, 2),
                speed = Math.Round(maxResutlAll.value / (DateTime.Parse(this.curTime) - DateTime.Parse(this.originTime)).Days, 3)
            };
            //累积最小变化
            var minValueAllTheTime = this.curValues.Where(p => double.IsNaN(p.value) == false).Min(g => Math.Abs(g.value));
            var minResutlAll = this.curValues.First(g => Math.Abs(g.value) == minValueAllTheTime);
            this.minPointAllTheTime = new summaryResult()
            {
                name = minResutlAll.name,
                value = Math.Round(minResutlAll.value, 2),
                speed = Math.Round(minResutlAll.value / (DateTime.Parse(this.curTime) - DateTime.Parse(this.originTime)).Days, 3)
            };


            //本次变形系列
            var curChanges = (from p in this.curValues
                              from q in this.lastValues
                              where p.name == q.name
                              select new result()
                              {
                                  name = p.name,
                                  value = p.value - q.value
                              }).ToList();

            //本次变形最大变化
            var maxValueThisTime = curChanges.Where(p => double.IsNaN(p.value) == false).Max(g => Math.Abs(g.value));
            var maxResutlThisTime = curChanges.First(g => Math.Abs(g.value) == maxValueThisTime);

            this.maxPointThisTime = new summaryResult()
            {
                name = maxResutlThisTime.name,
                value = Math.Round(maxResutlThisTime.value, 2),
                speed = Math.Round(maxResutlThisTime.value / (DateTime.Parse(this.curTime) - DateTime.Parse(this.lastTime)).Days, 3)
            };

            //本次变形小变化
            var minValueThisTime = curChanges.Where(p => double.IsNaN(p.value) == false).Min(g => Math.Abs(g.value));
            var minResutlThisTime = curChanges.First(g => Math.Abs(g.value) == minValueThisTime);

            this.minPointThisTime = new summaryResult()
            {
                name = minResutlThisTime.name,
                value = Math.Round(minResutlThisTime.value, 2),
                speed = Math.Round(minResutlThisTime.value / (DateTime.Parse(this.curTime) - DateTime.Parse(this.lastTime)).Days, 3)
            };

        }


    }

    public class summaryResult : result
    {
        public double speed { get; set; }
    }
}